package com.kael.sgs.server.entity;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;

import com.kael.sgs.core.Connection;
import com.kael.sgs.core.ServerConnection;
import com.kael.sgs.core.util.Log;
import com.recsmile.sgs.client.entity.WelcomeSession;

public class Server {
	public static final int DEFAULT_PORT = 12345;
	private static final String TAG = "Server";
	private final int mPort;
	private final ExecutorService executor;

	private WelcomeSession session;

	public Server(int port) {
		session = new WelcomeSession();
		mPort = port;
		executor = Executors.newCachedThreadPool();
	}

	/**
	 * initialize server with DEFAULT_PORT
	 */
	public Server() {
		session = new WelcomeSession();
		mPort = DEFAULT_PORT;
		executor = Executors.newCachedThreadPool();
	}

	/**
	 * Start the server
	 */
	public void start() {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(mPort);
			Log.log(TAG, "waiting for connections...");
			while (true) {
				final Socket socket = serverSocket.accept();
				executor.execute(
						new Runnable() {
							
							@Override
							public void run() {
								Log.log(TAG, "receiving new connection...");
								try {
									Connection connection = new ServerConnection(socket);
									session.onReceivedConnection(connection);
								} 
								catch (IOException e) {
									Log.error(TAG, "I/O Exception");
									e.printStackTrace();
								}
								
							}
					
			});
		} 
		}catch (IOException e) {
			Log.error(TAG, "error creating server socket");
			e.printStackTrace();
		} 
		catch (RejectedExecutionException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Server().start();
	}
}